package com.martinwj.task;

import com.martinwj.entity.Video;
import com.martinwj.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: Quartz
 * @Description: TODO Quartz定时任务类
 * @author: martin-wj
 * @createDate: 2020-12-17
 */
public class Quartz {

    @Autowired
    private VideoService videoService;

    /***
     * 定时任务
     * 清空视频点击量
     */
    public void execute() {
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);

        Video video = new Video();
        // 1.0 清空视频的今日点击量
        video.setViewCountDay("0");

        // 2.0 星期一，清空视频的周点击量
        int nWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
        if (nWeek==1) {
            video.setViewCountWeek("0");
        }

        // 3.0 每月1号，清空视频的月点击量
        int nMonth = calendar.get(Calendar.DAY_OF_MONTH);
        if (nMonth==1) {
            video.setViewCountMonth("0");

            // 4.0 每年1月1号，清空视频的年点击量
            int nYear = calendar.get(Calendar.MONTH);
            if (nYear==1) {
                video.setViewCountYear("0");
            }
        }

        videoService.clearViewCount(video);
    }
}
